package net.exoweb.animate.actions
{
    import flash.display.*;

    import net.exoweb.animate.actions.Action;
    import net.exoweb.animate.common.Maths;

    public class ChangeAlpha extends Action
    {
        private var _alpha:Number;

        public function ChangeAlpha(alpha:Number, speed:Number)
        {
            super(speed);
            _alpha = alpha;
        }

        public function get alpha():Number
        {
            return _alpha;
        }

        public function set alpha(v:Number):void
        {
            _alpha = v;
        }

        override public function update(obj:DisplayObject, time:Number):void
        {
            var da:Number = _alpha - obj.alpha;
            var dv:Number = time * _speed;
            if(((-Maths.EXP < da) && (da < Maths.EXP)) ||
               ((-dv < da) && (da < dv)))
            {
                obj.alpha = _alpha;
                complete();
            }
            else if(da > Maths.EXP)
            {
                obj.alpha += dv;
            }
            else if(da < -Maths.EXP)
            {
                obj.alpha -= dv;
            }
        }
    }
}
